package com.ascendere9.ff.pof.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;

import android.os.Environment;
import android.os.StatFs;

/**
 * @author: ThanhNH
 */
public class IOUtil {

	// ===================================================================
	// Constants
	// ===================================================================
	private static final String LOG_TAG = IOUtil.class.getSimpleName();

	// ===================================================================
	// Fields
	// ===================================================================

	// ===================================================================
	// Constructors
	// ===================================================================

	// ===================================================================
	// Getter & Setter
	// ===================================================================

	// ===================================================================
	// Methods from BaseClass & Interfaces
	// ===================================================================

	// ===================================================================
	// Methods
	// ===================================================================
	public static long getSdCardSizeAvailable() {
		StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
		long bytesAvailable = (long) stat.getBlockSize() * (long) stat.getBlockCount();
		return bytesAvailable;
	}

	public static File createFolder(String pPath) {
		File file = new File(pPath);
		if (!file.exists()) {
			file.mkdirs();
		}
		return file;
	}

	public static File createFile(String pPath, String pFileName) {
		File file = new File(pPath);
		if (!file.exists()) {
			file.mkdirs();
		}
		file = new File(pPath, pFileName);
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return file;
	}

	public static String inputStreamToString(InputStream is) {
		String data = null;
		try {
			Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
			data = IOUtil.readerToString(reader);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return data;
	}

	public static String fileToString(File pFile) {
		String data = null;
		try {
			Reader reader = new BufferedReader(new FileReader(pFile));
			data = IOUtil.readerToString(reader);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return data;
	}

	private static String readerToString(Reader pReader) throws IOException {
		String data = null;
		Writer writer = new StringWriter();
		char[] buffer = new char[1024];
		int n = 0;
		while ((n = pReader.read(buffer)) != -1) {
			writer.write(buffer, 0, n);
		}
		data = writer.toString();
		writer.close();
		return data;
	}

	// ===================================================================
	// Methods for SubClass
	// ===================================================================

	// ===================================================================
	// Inner and Anonymous Classes
	// ===================================================================
}
